#include "timerbtn.h"

TimerBtn::TimerBtn(QWidget *parent, int count)
    : QPushButton(parent)
    , _counter(count)
{
    _timer = new QTimer(this);
    int origin_counter = _counter;
    connect(_timer, &QTimer::timeout, this, [this, origin_counter]() {
        _counter--;
        if (_counter == 0) {
            this->_timer->stop();
            _counter = origin_counter;
            setText("获取");
            setEnabled(true);
            return;
        }
        setText(QString::number(_counter));
    });
}

TimerBtn::~TimerBtn()
{
    _timer->stop();
}

void TimerBtn::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        setText(QString::number(_counter));
        setEnabled(false);
        _timer->start(1000);
        emit clicked();
    }
    QPushButton::mouseReleaseEvent(event);
}
